// 6-1

#include <stdio.h>
#include <string.h>

struct Records {
    char name[16];
    char birth[9];
    char tele[21];
};

void my_sort(struct Records *address, int N) {
    for (int i = 0; i < N - 1; i++) {
        for (int j = i + 1; j < N; j++) {
            if (strcmp(address[i].name, address[j].name) > 0) {
                struct Records temp = address[i];
                address[i] = address[j];
                address[j] = temp;
            }
        }
    }
}

// 6-2
void menu(struct product p[], int n) {
    for (int i = 0; i < n; i++) {
        printf("[%d] %s %.1f\n", p[i].id, p[i].name, p[i].price);
    }
}

void order(struct product p[], int n) {
    int id, qty;
    double total = 0;
    while (1) {
        scanf("%d", &id);
        if (id == 0) break;
        scanf("%d", &qty);
        for (int i = 0; i < n; i++) {
            if (p[i].id == id) {
                double cost = p[i].price * qty;
                total += cost;
                printf("%s * %d = %.1f\n", p[i].name, qty, cost);
                break;
            }
        }
    }
    printf("Total = %.1f\n", total);
}


// 6-3

#include <string.h>
#include <math.h>

struct Score {
    int id;
    char name[11];
    int value[17];
    double finalScore;
    int rank;
};

void GetScore(struct Score grade[], int n) {
    for (int i = 0; i < n; i++) {
        scanf("%d %s", &grade[i].id, grade[i].name);
        for (int j = 0; j < 17; j++) {
            scanf("%d", &grade[i].value[j]);
        }
    }
}

void CalcuScore(struct Score grade[], int n) {
    for (int i = 0; i < n; i++) {
        int sum = 0, max = grade[i].value[0], min = grade[i].value[0];
        for (int j = 0; j < 17; j++) {
            int v = grade[i].value[j];
            sum += v;
            if (v > max) max = v;
            if (v < min) min = v;
        }
        grade[i].finalScore = (sum - max - min) / 15.0;
    }
}

void Ranking(struct Score grade[], int n) {
    for (int i = 0; i < n - 1; i++) {
        int maxIdx = i;
        for (int j = i + 1; j < n; j++) {
            if (grade[j].finalScore > grade[maxIdx].finalScore ||
                (fabs(grade[j].finalScore - grade[maxIdx].finalScore) < 1e-6 && grade[j].id < grade[maxIdx].id)) {
                maxIdx = j;
            }
        }
        if (maxIdx != i) {
            struct Score temp = grade[i];
            grade[i] = grade[maxIdx];
            grade[maxIdx] = temp;
        }
        grade[i].rank = i + 1;
    }
    grade[n - 1].rank = n;
}

void PrintScore(struct Score grade[], int n) {
    for (int i = 0; i < n; i++) {
        printf("第%d名 %d %s %6.2f\n", grade[i].rank, grade[i].id, grade[i].name, grade[i].finalScore);
    }
}
